package interface_JDK_0918.comparable;
/**
 * JDK中常用的接口
 * 1.java.lang.Comparable
 * 当student类实现了Comparable接口，根据compareTo方法的返回值就知道比较哪个属性的大小
 */
public class Student implements Comparable{
    private String name;
    private int score;

    public Student() {
    }

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    @Override
    public int compareTo(Object o) {
        //向下转型，还原o的Student引用
        //因此此时需要比较当前对象this和传入对象O的score的大小
        Student stu=(Student) o;
        if(o instanceof Student){//升序
            if(this.score>stu.score){
                return 1;
            } else if (this.score< stu.score) {
                return -1;
            }
            else
                return 0;
        }
        return 0;
    }

    public void showStu() {
        System.out.println("姓名：" + this.name + "   分数:" + this.score);
    }
}
